package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-03
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Api(tags = "课程章节管理")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation("根据课程id查询课程的章节和小节信息")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.findChapterVideoById(courseId);
        return R.ok().data("list",list);
    }

    @ApiOperation("添加课程章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        boolean save = eduChapterService.save(eduChapter);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("更新课程章节")
    @PutMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        boolean update = eduChapterService.updateById(eduChapter);
        if (update){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("根据id删除课程章节")
    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id) {
        boolean b = eduChapterService.deleteChapter(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("根据id查询课程章节")
    @GetMapping("getChapter/{id}")
    public R getChapter(@PathVariable String id) {
        EduChapter eduChapter = eduChapterService.getById(id);
        return R.ok().data("eduChapter",eduChapter);

    }
}

